package com.zlx.mall.basic.datasource.model;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.zlx.mall.basic.common.model.BaseObject;
import com.zlx.mall.basic.datasource.constant.DatabaseConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * 分页查询请求
 *
 * @author qingke
 **/
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class QueryPageRequest extends BaseObject
        implements java.io.Serializable {

    /** Current Page */
    @JsonAlias(value = {"page", "current", "currentPage", "pageNum"})
    private Long current = 1L;
    /** Page Size */
    @JsonAlias(value = {"size", "pageSize"})
    private Long size = 10L;

    public Long getPageStart() {
        return getOffset();
    }

    public void setSize(Long size) {
        this.size = size == null || size > DatabaseConstants.QUERY_MAX_SIZE ?
                DatabaseConstants.QUERY_MAX_SIZE : size;
    }

    public void setPageSize(Long size) {
        setSize(size);
    }

    public Long getOffset() {
        long current = getCurrent();
        if (current <= 1L) {
            return 0L;
        }
        return (current - 1) * getSize();
    }

    public <T> IPage<T> buildPage() {
        return buildPage(true);
    }

    public <T> IPage<T> buildPage(boolean isSearchCount) {
        return new Page<>(current, size, isSearchCount);
    }

}
